<?php
namespace app\dboard\controller;
use think\Controller;
use think\facade\Cookie;
use think\facade\Session;
use think\facade\Url;
use think\Db;
class Base extends Controller{
    public $res_data=['Code'=>1,'Msg'=>'','Sql'=>'','Data'=>[]];
    public $AdminAccount=[];
    public function initialize(){
        // if(!Session::has('AdminAccount.AdminID')){
        //     $cookievalue=Cookie::get('ZSyadminauths');
        //     if(strlen($cookievalue)>2){
        //         $destr=uc_authcode($cookievalue,'DECODE','ZSyadmin');
        //         list($AdminID, $AdminName,$AdminPhone,$AdminToken,$sid) = explode("\t",$destr);
        //         if(intval($AdminID) > 0){
        //             $this->AdminAccount['AdminID']=$AdminID;
        //             $this->AdminAccount['AdminName']=$AdminName;
        //             $this->AdminAccount['AdminPhone']=$AdminPhone;
        //             $this->AdminAccount['AdminToken']=$AdminToken;
        //             $this->AdminAccount['sid']=$sid;//默认0
        //             Session::set('AdminAccount',$this->AdminAccount);
        //         }
        //     }
        // }else{
        //     $this->AdminAccount=Session::get('AdminAccount');
        // }
        if(Session::has('AdminAccount.AdminID')){
            $this->AdminAccount=Session::get('AdminAccount');
        }
        if(isset($this->AdminAccount['AdminID'])){
            // 单点登录验证
            if (isset($this->AdminAccount['sid']) && intval($this->AdminAccount['sid'])>0) {
                $returnUrl=Url::build('dboard/mlogin/sign');
                $token = Db::name('store')->where('id',$this->AdminAccount['AdminID'])->value('token');
            } else {
                $returnUrl=Url::build('dboard/alogin/sign');
                $token = Db::name('admin_user')->where('id',$this->AdminAccount['AdminID'])->value('token');
            }
            if (!$this->AdminAccount['AdminToken'] || $token!=$this->AdminAccount['AdminToken']) {
                Session::clear();
                Cookie::delete('ZSyadminauths');
                Cookie::clear();
                if($this->request->isAjax()){
                    return $this->print_result(['Code'=>9999,'Msg'=>'请登录']);
                }
                $this->redirect($returnUrl);
            }
            $login_name = empty($this->AdminAccount['NickName'])?$this->AdminAccount['AdminName']:$this->AdminAccount['NickName'];
            $sid = 0;
            if(isset($this->AdminAccount['sid']))
              $sid = $this->AdminAccount['sid'];
            $this->assign('sid',$sid);
            $this->assign('LoginName',$login_name);
            $this->assign('adminInfo',$this->AdminAccount);
        }
    }
    /***获取省份***/
    public function getProvince($input){
        $province_id  = isset($input['province_id'])?intval($input['province_id']):0;
        if($province_id){
            $province=Db::name('province')->where('province_id',$province_id)->select();
        }else{
            $province=Db::name('province')->where("1=1")->select();
        }
        $this->res_data["Code"] = 0;
        $this->res_data["Data"] = $province;
        return $this->print_result($this->res_data);
    }
    /***获取市***/
    public function getCity($input){
        $province_id  = isset($input['province_id'])?intval($input['province_id']):0;
        $city_id      = isset($input['city_id'])?intval($input['city_id']):0;
        if(empty($province_id)){
            $this->res_data["Msg"] = '缺少省份编号';
            return $this->print_result($this->res_data);
        }
        if($city_id){
            $city=Db::name('city')->where([['father_id','=',$province_id],['city_id','=',$city_id]])->select();
        }else{
            $city=Db::name('city')->where('father_id',$province_id)->select();
        }
        $this->res_data["Code"] = 0;
        $this->res_data["Data"] = $city;
        return $this->print_result($this->res_data);
    }
    /***获取地区***/
    public function getArea($input){
        $city_id      = isset($input['city_id'])?intval($input['city_id']):0;
        $area_id      = isset($input['area_id'])?intval($input['area_id']):0;
        if(empty($city_id)){
            $this->res_data["Msg"] = '缺少市编号';
            return $this->print_result($this->res_data);
        }
        if($area_id){
            $area=Db::name('area')->where([['father_id','=',$city_id],['area_id','=',$area_id]])->select();
        }else{
            $area=Db::name('area')->where('father_id',$city_id)->select();
        }
        $this->res_data["Code"] = 0;
        $this->res_data["Data"] = $area;
        return $this->print_result($this->res_data);
    }
    public function print_result($res_data){
        if(!is_array($res_data)){
            $res_data['Code'] = 1;
            $res_data['Msg'] = '输出信息不符合规范';
        }
        if (!isset($res_data['Code']))
            $res_data['Code'] = 1;
        return json($res_data)->code(200)->header(['Content-Type' => 'application/json']);
    }

}
